package com.wyait.manage.web.article;


import com.wyait.manage.pojo.Article;
import com.wyait.manage.pojo.User;
import com.wyait.manage.service.ArticleService;
import com.wyait.manage.utils.PageDataResult;
import com.wyait.manage.web.menu.MenuController;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    private static final Logger logger = LoggerFactory
            .getLogger(MenuController.class);


    /**
     * 进入文章管理页面
     */
    @RequestMapping("/articleList")
    public String menuList(){
        return "article/articleList";
    }

     /**
     * 进入新增文章页面
     */
    @RequestMapping("/add")
    public String add(){
        return "article/add";
    }

    /**
     * 进入文章修改页面
     */
    @RequestMapping("/update")
    public String update(){
        return "article/update";
    }


    /**
     * 分页查询文章列表支持筛选加分页
     */
    @RequestMapping("/getArticleList")
    @ResponseBody
    public PageDataResult getArticleList(@RequestParam("page") Integer page,
                                      @RequestParam("limit") Integer limit, Article article){
        logger.debug("分页查询文章列表！搜索条件：article：" + article + ",page:" + page
                + ",每页记录数量limit:" + limit);
        PageDataResult pdr = new PageDataResult();
        try {
            if (null == page) {
                page = 1;
            }
            if (null == limit) {
                limit = 10;
            }
            // 获取导航菜单列表
            pdr = articleService.getArticleList(article, page, limit);
            logger.debug("文章列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("文章列表查询异常！", e);
        }

        return pdr;
    }


    /**
     * 文章[新增或更新]
     */
    @PostMapping("/setArticle")
    @ResponseBody
    public String setArticle(Article article){
        logger.debug("设置文章[新增或更新]！article:" + article );
        try {
            if (article.getContent() == null || article.getContent() =="") {
                logger.debug("文章[新增或更新]，结果=请您填写文章内容");
                return "您填写文章信息内容";
            }
            if (null == article) {
                logger.debug("文章[新增或更新]，结果=请您填写文章信息");
                return "您填写文章信息";
            }
            User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
            if (null == currentUser) {
                logger.debug("设置文章[新增或更新]，结果=您未登录或登录超时，请您登录后再试");
                return "您未登录或登录超时，请您登录后再试";
            }

            // 设置用户[新增或更新]
            logger.info("设置文章[新增或更新]成功！article=" + article + "，操作的用户ID=" + currentUser.getId());
            return articleService.setArticle(currentUser,article);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("设置文章[新增或更新]异常！", e);
            return "操作异常，请您稍后再试";
        }
    }


    /**
    *根据id删除文章
     */
    @PostMapping("/delArticle")
    @ResponseBody
    public String delArticle(Integer id){
        logger.debug("删除文章！id:" + id );
        try{
            User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
            if (null == currentUser) {
                logger.debug("设置删除文章，结果=您未登录或登录超时，请您登录后再试");
                return "您未登录或登录超时，请您登录后再试";
            }
            return articleService.delArticle(currentUser,id);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("设置文章[删除]异常！", e);
            return "操作异常，请您稍后再试";
        }

    }

    /**
     *根据id查询文章
     */
    @GetMapping("/getArticle")
    @ResponseBody
    public Article getArticleById(Integer id){
        logger.debug("查询文章！id:" + id );
        Article article = new Article();
        try{
            article = articleService.getArticleById(id);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("设置文章[查询]异常！", e);
        }
        return article;
    }


}
